function checkTree(field){
	if (field.checked) {
		checkPais(field);
		checkFilhos(field);
	} else {
		uncheckFilhos(field);
	}
}

 function maskFormatDate(component,componentFocus){
			var date = component.value;
			var size = date.length;
			if(  size > 2 ){
				var element = date.substring(2,3);
				if( element != "/" ){
					date = date.substring(0,2) + "/" + date.substring(2);  
					component.value = date;
					return;	}}
			if(  size > 5 ){
				var element = date.substring(5,6);
				if( element != "/" ){
					date = date.substring(0,5) + "/" + date.substring(5);  
					component.value = date;
					return;	}}
		    
		    if(size > 9){
		    	if (doDate(component)){
		    		componentFocus.focus();
		    		
		    	}	
		    
		    }
				
		}

function soNumeroBarra(campo) {
			
			  var caracteresIvalidos = "1234567890/";
			  for (i = 0; i < campo.value.length; i++) {
			    if (caracteresIvalidos.indexOf(campo.value.charAt(i)) == -1) {
			      temp1 = campo.value.substr(0, i);
			      temp2 = campo.value.substr(i + 1, campo.value.length);
			      campo.value = temp1 + temp2;
			      i = 0;
			    }
			  }
			}
		
function trim(str) {
    return str.replace(/[\s]/g,'')
}

function checkPais(field) {
	
	id = field.id;
	
	array = field.id.split(':');
	
	element = '';
	
	if (array.length > 5) {
	
		fim = id.substring(id.lastIndexOf(':', id.length));
		
		id = id.replace(fim, '');
		
		while(id.split(':').length > 4) {
			
			id = id.substring(0,id.lastIndexOf(':'));
			
			element = document.getElementById(id + ':pai');
			
			if (element) {
			
				element.checked = field.checked;
				
			}
		}
	
	}
}
	
function uncheckFilhos(field) {

	var element = '';
	
	var id = field.id;
	
	if (!field.disabled) {
		field.checked = false;
	}

	if (id.lastIndexOf('filho') == -1) {

		fim = id.substring(id.lastIndexOf(':', id.length)+1);
	
		id = id.replace(fim,'');

		if (document.getElementById(id+'0:pai')) {
		
			element = document.getElementById(id+'0:pai');

		} else {
		
			element = document.getElementById(id+'0:filho');
		}
		
		var i = 1;
		
		while (element) {

			uncheckFilhos(element);
			
			if (document.getElementById(id+i+':'+'pai')) {
			
				element = document.getElementById(id+i+':'+'pai');
				
			} else {
			
				element = document.getElementById(id+i+':'+'filho');
			}
			
			i++;
		}
	}
}

function doDate(pStr)
{
var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
var reDate = reDate4;

	eval("reDate = reDate5" );
	if (reDate.test(pStr.value)) {
		return true;
	} else if (pStr.value != null && pStr.value != "") {
		alert(pStr.value +  " Data incorreta.");
		pStr.value = '';
		pStr.focus();
		return false;
		//pStr.focus();

	}
}

function checkFilhos(field) {

	var element = '';
	
	var id = field.id;
	
	if (!field.disabled) {
		field.checked = true;
	}

	if (id.lastIndexOf('filho') == -1) {

		fim = id.substring(id.lastIndexOf(':', id.length)+1);
	
		id = id.replace(fim,'');

		if (document.getElementById(id+'0:pai')) {
		
			element = document.getElementById(id+'0:pai');

		} else {
		
			element = document.getElementById(id+'0:filho');
		}
		
		var i = 1;
		
		while (element) {

			checkFilhos(element);
			
			if (document.getElementById(id+i+':'+'pai')) {
			
				element = document.getElementById(id+i+':'+'pai');
				
			} else {
			
				element = document.getElementById(id+i+':'+'filho');
			}
			
			i++;
		}
	}
}
	
// Expande todos os n?s de uma tree2 do jsf
function expandAllTree (treeID) {

	imgs = document.getElementsByTagName('img');
	var hierarquia;
	
	for (i = 0;i<imgs.length;i++) {
	
		id = imgs[i].id;

		if (imgs[i].src.indexOf('nav-plus-line-middle.gif') > -1 ||
				imgs[i].src.indexOf('nav-plus-line-last.gif') > -1) {
				
			hierarquia = id.substring(0, id.lastIndexOf(':'));
			hierarquia = hierarquia.substring(hierarquia.indexOf(':')+1, hierarquia.length);
			hierarquia = hierarquia.substring(hierarquia.indexOf(':')+1, hierarquia.length);
			
			if (imgs[i].src.indexOf('nav-plus-line-last.gif') > -1) {
			
				imgPosition = 'last';
				
			} else if (imgs[i].src.indexOf('nav-plus-line-middle.gif') > -1) {
			
				imgPosition = 'middle';
			}
			
			treeNavClick('org.apache.myfaces.tree.TOGGLE_SPAN:'+id.substring(0, id.lastIndexOf(':')+1)+hierarquia, 
			id, 
			'/AdmPortalWeb/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/11713847/tree2.HtmlTreeRenderer/images/nav-plus-line-'+imgPosition+'.gif', 
			'/AdmPortalWeb/faces/myFacesExtensionResource/org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader/11713847/tree2.HtmlTreeRenderer/images/nav-minus-line-'+imgPosition+'.gif',
			'', '', '', treeID, hierarquia);
		}
	}
}

function confirmaExclusao() {

	return confirm("Deseja realmente excluir este registro?");
	
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function matricula(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que n?o ? d?gito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2")             //Coloca tra?o entre o quinto e o sexto d?gito
    return v
}